$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'production/erporderproduction/storelist',
        datatype: "json",
        colModel: [
            {
                label: '操作', width: 50, align: 'center',
                formatter: function (value, options, row) {
                    // 分配订单生产状态
                    var allotProductionStatus = row.allotProductionStatus;
                    // 分配订单收货状态 0:未收货，1:已收货
                    var allotReceivedStatus = row.allotReceivedStatus;
                    // 生产单财务审核状态:1.待审核,2.待付款,3.已付款,11.作废
                    var financeStatus = row.financeStatus;
                    if (allotReceivedStatus == 1 && financeStatus == 1){
                        return '<a href="javascript:;" onclick="return auditFinish(\'' + options.rowId + '\');">审核完成</a>';
                    }
                    return '--';
                }
            },
            {label: 'productionId', name: 'productionId', index: 'production_id', key: true,hidden:true, align: 'center'},
            {label: '原订单号', name: 'orderNo', index: 'order_no', width: 80, align: 'center',
                formatter: function (value) {
                    return '<a href="javascript:;" onclick="return window.erporderAllotPageVm.getInfo(\'' + value + '\',false);">'+ value +'</a>'
                }
            },
            {label: '门店名称', name: 'sellerName', index: 'sellerName', width: 80, align: 'center'},
            {label: '金额', name: 'payableAmount', index: 'payable_amount', width: 60, align: 'center'},
            {label: '产品名称', name: 'erpItemName', index: '', width: 60, align: 'center'},
            {label: '产品规格', name: 'addon', index: 'addon', width: 130, align: 'center',
                formatter: function (value) {
                    return vm.parsingAddon(value)
                }
            },
            {label: '分配时间', name: 'createTime', index: 'create_time', width: 100, align: 'center'},
            {label: '工序', name: 'attributeName', index: 'attribute_name', width: 50, align: 'center'},
            {label: '数量', name: 'nums', index: 'nums', width: 40, align: 'center'},
            {label: '单位', name: 'unit', index: 'unit', width: 40, align: 'center'},
            { label: '生产状态', name: 'allotProductionStatus', index: '', width: 80 , align:'center',
                formatter: function (value, options, row) {
                    var valueText = "--";
                    if (value == null || value == 1){
                        valueText = "待接单";
                    }else if (value == 3){
                        valueText = "生产中";
                    }else if (value == 4){
                        valueText = "生产完成";
                    }else if (value == 6){
                        valueText = "已完结";
                    }
                    return valueText;
                }
            },
            {
                label: '财务状态', name: 'financeStatus', index: 'finance_status', width: 50, align: 'center',
                formatter: function (value, options, row) {
                    var valueText = "";
                    var userData = $("#jqGrid").jqGrid('getGridParam', 'userData');
                    $.each(userData.financeStatusEnum, function (index, module) {
                        if (module.id == row.financeStatus) {
                            valueText = module.describe;
                            return false;
                        }
                    });
                    return valueText;
                }
            },
            {label: '提现方式', name: 'onlineCash', width: 80, align: 'center',
                formatter: function (value) {
                    return value == 0 ? '线下' : '线上';
                }
            },
            {label: '生产商', name: 'producerName', index: 'producerName', width: 120, align: 'center'},
            {label: '图片', name: 'sendPicsUrl', index: '', width: 35, align: 'center',
                formatter: function (value, options, row) {
                    if (isBlank(value)){
                        return '';
                    }
                    var urlArr = value.split(',');
                    var html = '<img class="img-thumbnail" src="'+window.parent.vm.ossHost +urlArr[0]+'">';
                    return html;
                }
            },
            {label: '跟单员', name: 'pmcName', width: 50, align: 'center'},
            {label: '第三方单号', name: 'productionOrder', width: 100, align: 'center'},
            {label: '总额', name: 'allotTotalAmount', index: '', width: 50, align: 'center'},
            {label: '调价', name: 'allotDiscount', index: '', width: 50, align: 'center'},
            {label: '应付金额', name: 'allotPayableAmount', index: '', width: 50, align: 'center'},
        ],
        viewrecords: true,
        height: window.innerHeight - 120,
        rowNum: 25,
        rowList: [25, 50, 100],
        rownumbers: true,
        rownumWidth: 40,
        autowidth: true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            // 隐藏grid底部滚动条
            // $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});

            var userData = $("#jqGrid").jqGrid('getGridParam', 'userData');
            vm.e.orderProductionStatusEnum = userData.orderProductionStatusEnum;
            vm.e.goodsTypeEnum = userData.goodsTypeEnum;
            vm.e.financeStatusEnum = userData.financeStatusEnum;
            vm.sellerInfoList = userData.sellerInfoList;
        }
    });
    // 显示序号
    $("#jqGrid").jqGrid('setLabel',0, '序号', '{width:80}');
});

/**
 * 审核完成
 */
function auditFinish(rowId) {
    var rowData = $("#jqGrid").jqGrid('getRowData', rowId);
    var orderNo = rowData.orderNo;
    var message = "单号：<span class='text-primary'>"+ orderNo +"</span> 是否审核完成？";
    confirm(message, function(){
        var productionId = rowData.productionId;
        $.ajax({
            type: "GET",
            url: baseURL + "production/erporderproduction/auditFinish",
            contentType: "application/json",
            data: {
                'productionId': productionId,
            },
            success: function(r){
                if(r.code == 0){
                    alert('操作成功', function(index){
                        vm.reload();
                    });
                }else{
                    alert(r.msg);
                }
            }
        });
    });
}

var vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            showOrder: false,
            showdeleteButton: false, // 是否显示订单详情页面的删除按钮 true:显示 false:不显示
            erpOrderProduction: {},
            erpOrder: {},
            orderInfo: {},
            csIdList: [],
            remarkList: [],
            erpOrderItemEntityList: [],
            erpOrderProductionList: [],
            userPaymentList: [], // 支付信息列表
            sellerInfoList: [],//自营门店列表
            pmcIdList:[], // 跟单员列表
            q: {
                orderNo: '',
                productionOrder: '',
                allotProductionStatus: '',
                financeStatus: '',
                producerName: '',
                producerType: '',
                pmcId: '',
                thirdMerchantId: ''
            },
            e: {
                orderProductionStatusEnum: [],
                goodsTypeEnum: [],
                financeStatusEnum: [],
            },
        },
        methods: {
            query: function () {
                vm.reload();
            },
            /* 查询重置 */
            reset: function () {
                vm.q.orderNo = '';
                vm.q.allotProductionStatus = '';
                vm.q.financeStatus = '';
                vm.q.productionOrder = '';
                vm.q.producerName = '';
                vm.q.producerType = '';
                vm.q.pmcId = '';
                vm.q.thirdMerchantId = '';
                vm.reload();
            },
            // getInfo: function(orderNo){
            //     $.get(baseURL + "order/erporder/allotInfo/" + orderNo, function(r){
            //         vm.orderInfo = r.orderInfo;
            //         vm.erpOrder = r.erpOrder;
            //         vm.csIdList = r.csIdList;
            //         vm.pmcIdList = r.pmcIdList;
            //         vm.remarkList = r.remarkList;
            //         vm.userPaymentList = r.userPaymentList;
            //         vm.erpOrderItemEntityList = r.erpOrderItemEntityList;
            //         vm.erpOrderProductionList = r.erpOrderProductionList;
            //         vm.showOrder = true;
            //         vm.showList = false;
            //     });
            // },

            /* 获取订单详情信息 */
            erporderdetails: function(value){
                window.erporderAllotPageVm.getInfo({
                    orderNo: vm.erpOrder.orderNo,
                    showdeleteButton: vm.showdeleteButton
                });
            },
            /* 格式化报价产品规格 */
            parsingAddon(addon){
                try {
                    if (typeof JSON.parse(addon) == "object"){
                        var jsonObj = JSON.parse(addon);
                        var content = '';
                        for (var a in jsonObj){
                            var partsName = jsonObj[a].partsName;
                            var text = partsName + '：';
                            var basicParam = jsonObj[a].basicParam;
                            if (!isBlank(basicParam)){
                                for (var b in basicParam){
                                    text += ' ' + basicParam[b];
                                }
                            }
                            var craftParam = jsonObj[a].craftParam;
                            if (!isBlank(craftParam)){
                                for (var c in craftParam){
                                    text += ' ' + craftParam[c];
                                }
                            }if (a == jsonObj.length - 1){
                                content += text
                            }else {
                                content += text + '，'
                            }
                        }
                        return content
                    }
                }catch (e){}
                return addon
            },
            reload: function (event) {
                vm.showList = true;
                vm.showOrder = false;
                var page = $("#jqGrid").jqGrid('getGridParam','page');
                $("#jqGrid").jqGrid('setGridParam', {
                    page: page,
                    postData: {
                        'orderNo': vm.q.orderNo,
                        'allotProductionStatus': vm.q.allotProductionStatus,
                        'financeStatus': vm.q.financeStatus,
                        'productionOrder': vm.q.productionOrder,
                        'producerName': vm.q.producerName,
                        'producerType': vm.q.producerType,
                        'pmcId': vm.q.pmcId,
                        'thirdMerchantId': vm.q.thirdMerchantId,
                    }
                }).trigger("reloadGrid");
            },
            reback: function () {
                vm.showList = true;
                vm.showOrder = false;
            },
        },
    })
;
